package com.ruoyi.edubuy.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.edubuy.domain.ShowBuyOrder;
import com.ruoyi.edubuy.domain.ShowShopping;
import com.ruoyi.edubuy.service.IShowBuyOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * 订单查询Controller
 *
 * @author lsj
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/edubuy/order")
public class ShowBuyOrderController extends BaseController {
    private String prefix = "edubuy/order";

    @Autowired
    private IShowBuyOrderService showBuyOrderService;

    @RequiresPermissions("edubuy:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询订单查询列表
     */
    @RequiresPermissions("edubuy:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShowBuyOrder showBuyOrder) {
        startPage();
        List<ShowBuyOrder> list = showBuyOrderService.selectShowBuyOrderList(showBuyOrder);
        // 只能查询自己的订单编号
        Iterator<ShowBuyOrder> iterator = list.iterator();
        while (iterator.hasNext()){
            if (!iterator.next().getOrderName().equals((String) PermissionUtils.getPrincipalProperty("userName"))) {
                iterator.remove();
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出订单查询列表
     */
    @RequiresPermissions("edubuy:order:export")
    @Log(title = "订单查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShowBuyOrder showBuyOrder) {
        List<ShowBuyOrder> list = showBuyOrderService.selectShowBuyOrderList(showBuyOrder);
        ExcelUtil<ShowBuyOrder> util = new ExcelUtil<ShowBuyOrder>(ShowBuyOrder.class);
        return util.exportExcel(list, "订单查询数据");
    }

    /**
     * 新增订单查询
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单查询
     */
    @RequiresPermissions("edubuy:order:add")
    @Log(title = "订单查询", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShowBuyOrder showBuyOrder) {
        return toAjax(showBuyOrderService.insertShowBuyOrder(showBuyOrder));
    }

    /**
     * 修改订单查询
     */
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Integer orderId, ModelMap mmap) {
        ShowBuyOrder showBuyOrder = showBuyOrderService.selectShowBuyOrderById(orderId);
        mmap.put("showBuyOrder", showBuyOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单查询
     */
    @RequiresPermissions("edubuy:order:edit")
    @Log(title = "订单查询", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShowBuyOrder showBuyOrder) {
        return toAjax(showBuyOrderService.updateShowBuyOrder(showBuyOrder));
    }

    /**
     * 删除订单查询
     */
    @RequiresPermissions("edubuy:order:remove")
    @Log(title = "订单查询", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(showBuyOrderService.deleteShowBuyOrderByIds(ids));
    }
}
